package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsGoodsService;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品管理
 * @author lixu
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/goods")
public class GoodsController {


    @Autowired
    private DtsGoodsService dtsGoodsService;
    /**
     * 商品分页查询
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "查询")
    @RequiresPermissions(value = "admin:goods:list")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
            String sort,
            String order
    ){
        DtsAdmin dtsAdmin = AuthSupport.currentUser();
        IPage<DtsGoods> ipage = dtsGoodsService.list(page,limit,sort,order,dtsAdmin);
        Map<String,Object> data =  new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * ##### 2. 商品修改回显
     *
     * - 接口地址 : http://localhost:8083/admin/goods/detail?id=23858
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "编辑")
    @RequiresPermissions(value = "admin:goods:detail")
    @GetMapping("/detail")
    public Object detail(Integer id){

        Map<String,Object> data = dtsGoodsService.findDetailByGoodId(id);


        return ResponseUtil.ok(data);

    }
    /**
     * ##### 1.3. 添加修改中品牌和分类回显
     *
     * - 接口地址 : http://localhost:8083/admin/goods/catAndBrand
     */
    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        Map<String,Object> data = dtsGoodsService.catAndBrand();
        return ResponseUtil.ok(data);

    }

    /**
     * 更新商品
     * ##### 9.3.1.4. 商品修改保存
     *
     * - 接口地址 : http://localhost:8083/admin/goods/update
     */
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "更新")
    @RequiresPermissions(value = "admin:goods:update")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){

        dtsGoodsService.update(goodsAllinone);

        return ResponseUtil.ok();
    }
}
